package classes.controlers {
	import classes.managers.Game2_Manager;
	import classes.sounds.SoundCollection;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.events.MouseEvent;
	import flash.utils.Timer;
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class Click_Gauge_Controler {
		
		private var gm:Game2_Manager;
		private var mc:MovieClip;
		
		private var origineSpeed:Number = 8;
		private var speed:Number = 10;
		private var pointTimer:Timer;
		
		private var gaugeTimer:Timer;
		private var gaugeTime:int = 10;
		
		private var oldHitX:Number;
		private var origineWidth:Number = 80;
		
		private var clicked:Boolean = false;
		private var justOneClick:Boolean = true;
		private var comboCnt:int = 0;
		
		public function Click_Gauge_Controler( _gm:Game2_Manager, _mc:MovieClip ) {
			gm = _gm;
			mc = _mc;
			
			oldHitX = mc.field_mc.hit_area.x;
			mc.pointCopy_mc.visible = false;
		}
		
		public function init():void {
			mc.point_mc.x = 0;
			
			mc.pointCopy_mc.visible = false;
			mc.pointCopy_mc.x = 0;
			
			mc.field_mc.hit_area.x = oldHitX;
			mc.field_mc.hit_area.width = origineWidth;
			
			
			justOneClick = true;
			speed = origineSpeed;
			comboCnt = 0;
			
			//포인터속도 조절하는 타이머 -------------------------------------
			pointTimer = new Timer(30);
			pointTimer.addEventListener(TimerEvent.TIMER, pointTimerHandler );
			
			//전체 시간 10초 ----------------------------------
			gaugeTimer = new Timer( 1000, gaugeTime );
			gaugeTimer.addEventListener(TimerEvent.TIMER, gaugeTimerHandler );
			gaugeTimer.addEventListener(TimerEvent.TIMER_COMPLETE, gaugeTimerCompleteHandler );
			
			mc.field_mc.hit_area.buttonMode = true;
			mc.field_mc.hit_area.addEventListener( MouseEvent.MOUSE_DOWN, MOUSE_DOWN );
			mc.parent["extract_mc"].buttonMode = true;
			mc.parent["extract_mc"].addEventListener( MouseEvent.MOUSE_DOWN, MOUSE_DOWN );
			
			start();
		}
		
		private function start():void {
			pointTimer.start();
			gaugeTimer.reset();
			gaugeTimer.start();
		}
		
		private function pointTimerHandler(e:TimerEvent):void {
			mc.point_mc.x += speed;
			if ( mc.point_mc.x >= mc.field_mc.width + mc.field_mc.x) {
				mc.point_mc.x = mc.field_mc.width + mc.field_mc.x;
				speed *= -1;
				clicked = false;
			}
			if ( mc.point_mc.x <= mc.field_mc.x ) {
				mc.point_mc.x = mc.field_mc.x;
				speed *= -1;
				clicked = false;
			}
			
			if ( mc.field_mc.hit_area.hitTestObject( mc.point_mc ) ) {
				mc.click_mc.visible = true;
			} else{
				mc.click_mc.visible = false;
			}
		}
		
		private function gaugeTimerHandler(e:TimerEvent):void {
			mc.timer_mc.timer_txt.text = String( gaugeTime - gaugeTimer.currentCount );
		}
		
		private function gaugeTimerCompleteHandler(e:TimerEvent):void {
			gaugeTimer.stop();
			pointTimer.stop();
			pointTimer.reset();
			
			gaugeTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, gaugeTimerCompleteHandler );
			pointTimer.removeEventListener(TimerEvent.TIMER, pointTimerHandler );
			mc.field_mc.hit_area.removeEventListener( MouseEvent.MOUSE_DOWN, MOUSE_DOWN );
			
			mc.pointCopy_mc.visible = false;
			mc.pointCopy_mc.x = 0;
			mc.point_mc.x = 0;
			
			clicked = false;
			
			//mc.field_mc.hit_area.width = origineWidth;
			//mc.field_mc.hit_area.x = oldHitX;
			trace("000")
			clearGame();
		}
		
		
		private function MOUSE_DOWN(e:MouseEvent):void {
			if ( !clicked ) {
				clicked = true;
				mc.pointCopy_mc.visible = true;
				mc.pointCopy_mc.x = mc.point_mc.x;
				mc.pointCopy_mc.y = mc.point_mc.y;
				
				if ( mc.field_mc.hit_area.hitTestObject( mc.pointCopy_mc ) ) {
					mc.click_1.play();
					mc.parent["extract_mc"].play();
					
					if ( comboCnt == 0 ) {
						bonusAttack();
					}else if (comboCnt == 1) {
						combo1();
					}else if (comboCnt == 2) {
						combo2();
					}else if (comboCnt == 3) {
						combo3();
					}else if (comboCnt == 4) {
						combo4();
					}else if (comboCnt >= 5) {
						combo5();
					}
				}else {
					attackFailure();
				}
			}
		}
		
		private function attackFailure():void {
			//mc.field_mc.hit_area.width = origineWidth;
			//mc.field_mc.hit_area.x = oldHitX;
			if ( speed < 0 ) {
				speed = origineSpeed*-1;
			}else {
				speed = origineSpeed;
			}
			
			//comboCnt--;
			clearGame();
			gm.getJuice(comboCnt);
		}
		
		private function bonusAttack():void {
			var oldWidth:Number = mc.field_mc.hit_area.width;
			mc.field_mc.hit_area.width = mc.field_mc.width * .3;
			mc.field_mc.hit_area.x += (oldWidth - mc.field_mc.hit_area.width) *.5;
			
			comboCnt++;
			
			mc.parent["cup_mc"].gotoAndPlay("일");
			gm.getJuice(comboCnt);
			gm.getCoinAni(comboCnt);
			gm.getScore();
			
			SoundCollection.combo1Sound();
		}
		
		private function combo1():void {
			var oldWidth:Number = mc.field_mc.hit_area.width;
			mc.field_mc.hit_area.width = mc.width * .25;
			mc.field_mc.hit_area.x += (oldWidth - mc.field_mc.hit_area.width) *.5;
			
			speed *= 1.1;
			
			comboCnt++;
			mc.parent["cup_mc"].gotoAndPlay("이");
			gm.getJuice(comboCnt);
			gm.getCoinAni(comboCnt);
			gm.getScore();
			
			SoundCollection.combo2Sound();
		}
		
		private function combo2():void {
			var oldWidth:Number = mc.field_mc.hit_area.width;
			mc.field_mc.hit_area.width = mc.width * .2;
			mc.field_mc.hit_area.x += (oldWidth - mc.field_mc.hit_area.width) *.5;
			
			speed *= 1.2;
			
			comboCnt++;
			mc.parent["cup_mc"].gotoAndPlay("삼");
			gm.getJuice(comboCnt);
			gm.getCoinAni(comboCnt);
			gm.getScore();
			
			SoundCollection.combo3Sound();
		}
		
		private function combo3():void {
			var oldWidth:Number = mc.field_mc.hit_area.width;
			mc.field_mc.hit_area.width = mc.width * .15;
			mc.field_mc.hit_area.x += (oldWidth - mc.field_mc.hit_area.width) *.5;
			
			speed *= 1.25;
			
			comboCnt++;
			mc.parent["cup_mc"].gotoAndPlay("사");
			gm.getJuice(comboCnt);
			gm.getCoinAni(comboCnt);
			gm.getScore();
			
			SoundCollection.combo4Sound();
		}
		
		private function combo4():void {
			var oldWidth:Number = mc.field_mc.hit_area.width;
			mc.field_mc.hit_area.width = mc.width * .1;
			mc.field_mc.hit_area.x += (oldWidth - mc.field_mc.hit_area.width) *.5;
			
			speed *= 1.3;
			
			comboCnt++;
			mc.parent["cup_mc"].gotoAndPlay("오");
			gm.getJuice(comboCnt);
			gm.getCoinAni(comboCnt);
			gm.getScore();
			
			SoundCollection.combo4Sound();
		}
		
		private function combo5():void {
			var oldWidth:Number = mc.field_mc.hit_area.width;
			mc.field_mc.hit_area.width = mc.width * .1;
			mc.field_mc.hit_area.x += (oldWidth - mc.field_mc.hit_area.width) *.5;
			
			speed *= 1.15;
			
			comboCnt++;
			gm.getJuice(comboCnt);
			gm.getCoinAni(comboCnt);
			gm.getScore();
			
			SoundCollection.combo4Sound();
			
			if ( comboCnt == 10 ) clearGame();
		}
		
		
		private function clearGame():void {
			gaugeTimer.stop();
			pointTimer.stop();
			removeEvent();
			
			var timer:Timer = new Timer( 500, 1 );
			timer.addEventListener(TimerEvent.TIMER_COMPLETE, function (e:TimerEvent):void {
				timer.removeEventListener(TimerEvent.TIMER_COMPLETE, arguments.callee );
				gm.feedBack();
				SoundCollection.clearSound();
				trace("clearGame");
			});
			timer.start();
		}
		
		public function removeEvent():void {
			if ( gaugeTimer != null ) {
				if (gaugeTimer.hasEventListener(TimerEvent.TIMER_COMPLETE )) {
					gaugeTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, gaugeTimerCompleteHandler );
				}
			}
			if ( pointTimer != null ) {
				if (pointTimer.hasEventListener(TimerEvent.TIMER )) {
					pointTimer.removeEventListener(TimerEvent.TIMER, pointTimerHandler );
				}
			}
			
			if ( mc.field_mc.hit_area.hasEventListener( MouseEvent.MOUSE_DOWN ) ) {
				mc.field_mc.hit_area.removeEventListener( MouseEvent.MOUSE_DOWN, MOUSE_DOWN );
			}
		}
	}
}